<?php
class N1816ProjectsController extends AppController {

	var $name = 'N1816Projects';
	
	public function beforeFilter() {
            parent::beforeFilter();
            //$this->Auth->allow('index', 'view');
        }
	
	function index()
	{
		$this->N1816Project->recursive = 0;		
		
		$city_id = $this->Session->read("c_city_id");
		$year = $this->Session->read("c_year");
		$this->layout = "home";
		
		$conditions = array();		
		if($city_id)
		{
			$conditions["N1816Project.city_id"] = $city_id;
		}		
		if($year)
		{
			$conditions["N1816Project.year"] = $year;
		}
		
		if(count($conditions))
		{
			$this->paginate = array(
				'conditions' => $conditions				
			);
		}
		
		
		$this->set('n1816Projects', $this->paginate());
	}
	
	function view($id = null) {
		$this->layout = "home";
		if (!$id) {
			$this->Session->setFlash(__('Invalid n1816 project', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('n1816Project', $this->N1816Project->read(null, $id));
	}
	

	function manager_index() {
		$this->N1816Project->recursive = 0;		
		
		$user = $this->Auth->user();
		
		$cities = $this->N1816Project->City->citiesByUser($user["User"]["id"]);
		//var_dump($cities);
		
		$c_year = $this->Session->read("c_year");
		
		//get cities string
		foreach($cities as $city)
		{
			$arr_city_id[] = $city["City"]["id"];
		}
		//var_dump($arr_city_id);
		$str_cities = implode(",", $arr_city_id);
		//echo $str_cities;
		
		$this->paginate = array(
				'conditions' => array('city_id' => $arr_city_id)				
			);
		
		
		$conditions = array();
		$city_id = !empty($this->params['form']['filter_city']) && $this->params['form']['filter_city'] != "" ? $this->params['form']['filter_city'] : 0;
		if($city_id)
		{
			$conditions["N1816Project.city_id"] = $city_id;
		}
		
		//filter year
		$year = !empty($this->params['form']['filter_year']) && $this->params['form']['filter_year'] != "" ? $this->params['form']['filter_year'] : $c_year;
		if($year)
		{
			$conditions["N1816Project.year"] = $year;
		}
		
		if(count($conditions))
		{
			$this->paginate = array(
				'conditions' => $conditions				
			);
		}
		
		
		$this->set('cities', $cities);
		$this->set('city_id', $city_id);		
		$this->set('n1816Projects', $this->paginate());
		$this->set('year', $year);
	}

	function manager_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid n1816 project', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('n1816Project', $this->N1816Project->read(null, $id));
	}

	function manager_add() {
		$user = $this->Auth->user();
		Controller::loadModel('User');
		$year = $this->Session->read("c_year");
		
		
		if (!empty($this->data)) {
			
			//check if the user has accessibility to city
			if(!$this->User->isUserCity($user["User"]["id"], $this->data["N1816Project"]["city_id"]))
			{
				$this->Session->setFlash(__('Manager doesn\'t has role on this city', true));
				$this->redirect(array('action' => 'index'));
			}
			
			$year = $this->data['N1816Project']['year'];
			
			$this->N1816Project->create();
			if ($this->N1816Project->save($this->data)) {
				$this->Session->setFlash(__('Đề án đã được thêm', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The n1816 project could not be saved. Please, try again.', true));
			}
		}
		//$cities = $this->N1816Project->City->find('list');
		
		//get cities		
		$cs = $this->N1816Project->City->citiesByUser($user["User"]["id"]);
		foreach($cs as $city)
		{
			$cities[$city["City"]["id"]] = $city["City"]["name"];
		}	
		
		$this->set(compact('cities', 'year'));
	}

	function manager_edit($id = null) {
		
		$user = $this->Auth->user();			
		Controller::loadModel('User');
		
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid n1816 project', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			
			//check if the user has accessibility to city
			
			if(!$this->User->isUserCity($user["User"]["id"], $this->data["N1816Project"]["city_id"]))
			{
				$this->Session->setFlash(__('Manager doesn\'t has role on this city', true));
				$this->redirect(array('action' => 'index'));
			}	
			
			if ($this->N1816Project->save($this->data)) {
				$this->Session->setFlash(__('Đề án đã được lưu', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The n1816 project could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->N1816Project->read(null, $id);
			$year = $this->data['N1816Project']['year'];
		}
		//$cities = $this->N1816Project->City->find('list');
		
		//get cities		
		$cs = $this->N1816Project->City->citiesByUser($user["User"]["id"]);
		foreach($cs as $city)
		{
			$cities[$city["City"]["id"]] = $city["City"]["name"];
		}
		
		$this->set(compact('cities', 'year'));
	}

	function manager_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for n1816 project', true));
			$this->redirect(array('action'=>'index'));
		}
		
		//check if the user has accessibility to city
			$p = $this->N1816Project->read(null, $id);
			$user = $this->Auth->user();			
			Controller::loadModel('User');
			if(!$this->User->isUserCity($user["User"]["id"], $p["N1816Project"]["city_id"]))
			{
				$this->Session->setFlash(__('Manager doesn\'t has role on this city', true));
				$this->redirect(array('action' => 'index'));
			}
		
		if ($this->N1816Project->delete($id)) {
			$this->Session->setFlash(__('N1816 project deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('N1816 project was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
